package com.his.controller;


import com.his.commons.core.dto.PageInfo;
import com.his.commons.core.dto.Result;
import com.his.commons.core.exception.HisException;
import com.his.commons.web.util.ValidatedUtil;
import com.his.controller.form.ModifyDepartmentForm;
import com.his.controller.form.NewDepartmentForm;
import com.his.controller.form.PageForm;
import com.his.controller.info.DepartmentAndDisInfo;
import com.his.controller.info.DepartmentInfo;
import com.his.service.DepartmentService;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 梅津铧
 * @since 2022-03-22
 */
@RestController
@RequestMapping("/department")
@Api(tags = "科室接口")
@GlobalTransactional(rollbackFor = Exception.class)
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    /**
     * 创建科室及下属诊室
     * @param newDepartmentForm  科室及下属诊室信息
     * @param bindingResult
     * @throws HisException
     */
    @ApiOperation("创建科室及下属诊室")
    @PostMapping("/increaseDepaertment")
    public Result IncreaseDepartment(@RequestBody @Valid NewDepartmentForm newDepartmentForm, BindingResult bindingResult) throws HisException {
        ValidatedUtil.ValidatedException(bindingResult);

        boolean b = departmentService.increaseDepartment(newDepartmentForm);
        if(!b)  throw new HisException("创建科室及诊室失败",500);
        return Result.getInstance();
    }

    /**
     * 更正科室及诊室信息
     * @param modifyDepartmentForm  更正的科室及下属诊室信息
     * @param bindingResult
     * @throws HisException
     */
    @ApiOperation("更正科室及诊室信息")
    @PutMapping("/modifyDepartment")
    public Result ModifyDepartment(@RequestBody @Valid ModifyDepartmentForm modifyDepartmentForm, BindingResult bindingResult) throws HisException {
        ValidatedUtil.ValidatedException(bindingResult);

        boolean b = departmentService.modifyDepartment(modifyDepartmentForm);
        if(!b)  throw new HisException("更正科室及诊室信息失败",500);
        return Result.getInstance();
    }

    /**
     * 获取所有科室信息
     * @param pagenum 页码
     * @param pagesize 每页数据量
     * @return 目标页的科室信息
     * @throws HisException
     */
    @ApiOperation("获取所有科室信息")
    @GetMapping("/obtainDepartments")
    public Result<PageInfo<DepartmentInfo>> ObtainDepartments(
            @RequestParam(value = "pagenum",required = false) Integer pagenum,
            @RequestParam(value = "pagesize",required = false) Integer pagesize
    ) throws HisException {
        PageForm pageForm = new PageForm();
        pageForm.setPagenum(pagenum);
        pageForm.setPagesize(pagesize);

        PageInfo<DepartmentInfo> departmentInfoPageInfo  = departmentService.obtainDepartments(pageForm);
        if(null == departmentInfoPageInfo) throw new HisException("获取科室信息失败",500);
        return Result.getInstancePageInfo(DepartmentInfo.class).setData(departmentInfoPageInfo);
    }

    /**
     * 根据科室编号/名称获取科室信息
     * @param condition 科室编号/名称
     * @return  科室信息
     * @throws HisException
     */
    @ApiOperation("根据科室编号/名称获取科室信息")
    @GetMapping("/obtainDepartmentByCondition/{condition}")
    public Result<DepartmentInfo> ObtainDepartmentByCondition(@PathVariable("condition") String condition) throws HisException {

        DepartmentInfo departmentInfo = departmentService.obtainByCondition(condition);
        if(null == departmentInfo) throw new HisException("未获取到科室信息",500);
        return Result.getInstance(DepartmentInfo.class).setData(departmentInfo);
    }

    /**
     * 获取有诊室的科室信息
     * @return  有诊室的科室信息
     * @throws HisException
     */
    @ApiOperation("获取有诊室的科室信息")
    @GetMapping("/obtainDepartment")
    public Result<List<DepartmentAndDisInfo>> ObtainDepartment() throws HisException {

        List<DepartmentAndDisInfo> departmentInfos = departmentService.ObtainHasDis();
        if(departmentInfos.size()<=0) throw new HisException("未获取到出诊的科室信息",500);
        return Result.getInstanceList(DepartmentAndDisInfo.class).setData(departmentInfos);
    }

    /**
     * 获取无诊室的科室信息
     * @return  科室信息
     * @throws HisException
     */
    @ApiOperation("获取无诊室的科室信息")
    @GetMapping("/obtainDepartmentHasNot ")
    public Result<List<DepartmentAndDisInfo>> ObtainHasNotDis() throws HisException {

        List<DepartmentAndDisInfo> departmentInfos = departmentService.ObtainHasNotDis();
        if(departmentInfos.size()<=0) throw new HisException("未获取到出诊的科室信息",500);
        return Result.getInstanceList(DepartmentAndDisInfo.class).setData(departmentInfos);
    }

    /**
     * 解散科室及下属诊室
     * @param departmentId  科室id
     * @throws HisException
     */
    @ApiOperation("解散科室及下属诊室")
    @DeleteMapping("/logoutDepartment/{departmentId}")
    public Result LogoutDepartment(@PathVariable("departmentId") Integer departmentId) throws HisException {

        boolean b = departmentService.logoutById(departmentId);
        if(!b) throw new HisException("当前操作失败，请稍后重试",500);
        return Result.getInstance();
    }
}

